package com.tom.commonframework.common.refreshview.base;

import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tom.commonframework.R;

import static com.tom.commonframework.common.refreshview.constant.Constant.BEING_LOADED;
import static com.tom.commonframework.common.refreshview.constant.Constant.LAST_PAGE_NOT_DATA;
import static com.tom.commonframework.common.refreshview.constant.Constant.LOAD_MORE;


/**
 * Created by TomLeisen on 2018/7/27 下午1:55
 * Email: xy162162a@163.com
 * Description:
 */
public abstract class BaseLoadMoreAdapter<K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //线性
    public static final int ADAPTER_TYPE_LINEAR = 0x01;
    //网格
    public static final int ADAPTER_TYPE_GRID = 0x02;
    //底部加载更多
    private static final int ITEM_TYPE_LOAD = Integer.MAX_VALUE / 2;
    //是否显示加载更多,必须为true，否则会崩溃
    private boolean mShowLoadItem = true;
    //viewHolder
    private BaseLoadMoreViewHolder viewHolder;
    //默认类型
    private int mAdapterType = ADAPTER_TYPE_LINEAR;
    //数量
    private int mSpanCount;

    private OnClickLoadMoreListener clickLoadMoreListener;
    //是否最后一页
    private boolean isLastView;


    /**
     * 设置adapter的类型, 默认是线性的
     */
    public void setAdapterType(int type) {
        if (mAdapterType != type) {
            mAdapterType = type;
        }
    }

    /**
     * 网格布局的网格数量
     */
    public void setSpanCount(int count) {
        if (count != mSpanCount) {
            mSpanCount = count;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_LOAD) {
            if (viewHolder == null) {
                viewHolder = new BaseLoadMoreViewHolder(View.inflate(parent.getContext(), R.layout.refreshview_pull_to_refresh_footer3, null));
            }
            return viewHolder;
        } else {
            return onCreateViewHolders(parent, viewType);
        }
    }

    /**
     * 显示"加载更多"item, 并且position为末尾时,拦截
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mShowLoadItem && position == getItemCount() - 1) {
            // 最后一项 数据为0 的时候隐藏
            holder.itemView.setVisibility(getItemCounts() <= 0 ? View.GONE : View.VISIBLE);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickLoadMoreListener != null){
                        clickLoadMoreListener.onClickLoad();
                    }
                }
            });
        } else if (position < getItemCounts()) {
            holder.itemView.setVisibility(View.VISIBLE);
            onBindViewHolders((K) holder, position);
        } else {
            holder.itemView.setVisibility(View.INVISIBLE);
        }
    }

    public abstract RecyclerView.ViewHolder onCreateViewHolders(ViewGroup parent, int viewType);

    public abstract void onBindViewHolders(K holder, int position);

    public abstract int getItemCounts();


    @Override
    public int getItemCount() {
        if (mAdapterType == ADAPTER_TYPE_LINEAR) {
            return mShowLoadItem ? getItemCounts() + 1 : getItemCounts();
        } else {
            if (!mShowLoadItem)
                return getItemCounts();
            int remain = getItemCounts() % mSpanCount; // 余数
            if (remain == 0) {
                return getItemCounts() + 1;
            }
            return getItemCounts() + 1 + (mSpanCount - remain);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mShowLoadItem && position == getItemCount() - 1) {
            return ITEM_TYPE_LOAD;
        }
        return position;
    }

    /**
     * 当前是否属于正在加载状态
     * 除了 LoadMoreHelper 可以调用外，其他调用者均不能调用此方法
     * @param isLoading true 是 false 否
     */
    public void setLoadItemState(final boolean isLoading) {
        try {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    viewHolder.setLoadText(isLoading ? BEING_LOADED : LOAD_MORE);
                    viewHolder.setLoadPbVisibility(isLoading);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 是否是最后一页
     */
    public void setLastView(boolean isLastView){
        setLastView(isLastView,isLastView ? LAST_PAGE_NOT_DATA : LOAD_MORE);
    }

    /**
     * 是否是最后一页
     * @param isLastView  true 是  false 否
     * @param text  最后一页的显示文案
     */
    public void setLastView(final boolean isLastView, final String text){
        try {
            this.isLastView = isLastView;
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    viewHolder.setLoadText(text);
                    viewHolder.mLoadPb.setVisibility(View.GONE);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 是否是最后一页
     */
    public boolean isLastView() {
        return isLastView;
    }


    class BaseLoadMoreViewHolder extends RecyclerView.ViewHolder {

        TextView mLoadTv;
        ProgressBar mLoadPb;

        BaseLoadMoreViewHolder(View itemView) {
            super(itemView);
            mLoadTv = itemView.findViewById(R.id.pull_to_loading_text);
            mLoadPb = itemView.findViewById(R.id.pull_to_loading_progress);
        }

        void setLoadText(CharSequence text) {
            mLoadTv.setText(text);
        }

        void setLoadPbVisibility(boolean show) {
            mLoadPb.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 点击加载更多调用的接口
     */
    public interface OnClickLoadMoreListener {
        void onClickLoad();
    }

    public void setClickLoadMoreListener(OnClickLoadMoreListener clickLoadMoreListener) {
        this.clickLoadMoreListener = clickLoadMoreListener;
    }
}